/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.confiteriofuentes.modelo;

import com.confiteriafuentes.modelo.solicitud.Pedido;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;


/**
 *
 * @author Administrador
 */
public class PedidoDAO {

    




private Model model;
    private PreparedStatement psInsert;

    public PedidoDAO() throws SQLException {
        model = Model.getInstance();
    }

    public void insert(Pedido t) throws SQLException {
        if (psInsert == null) {
            String sqlInsert = "INSERT INTO pedidos (codigo_pedidos,codClie, fecha_pedido, fecha_entrega, subtotal,iva,total,estado) VALUES (?,?,?,?,?,?,?,?)";
            psInsert = (PreparedStatement) model.getCon().prepareStatement(sqlInsert);
        }

       psInsert.setInt(1,t.getCodigo());
       psInsert.setInt(2, t.getCliente().getCodigo());

       java.sql.Date fecha = new java.sql.Date(t.getFechadelpedido().getTime());

        psInsert.setDate(3, fecha);


         java.sql.Date fecha1 = new java.sql.Date(t.getFechaentrga().getTime());

        psInsert.setDate(4, fecha1);

        psInsert.setDouble(5, t.getSubtotal());

        psInsert.setDouble(6, t.getIva());

        psInsert.setDouble(7, t.getTotal());

        psInsert.setString(8, t.getEstado());

     

        psInsert.executeUpdate();




    }

       public boolean updateCodEnvio(int codenv,int cod) throws SQLException {

       PreparedStatement pst = null;
       boolean res = false;

          pst =model.getCon().prepareStatement("Update  pedidos set codEnvio = ? where codigo_pedidos = ? ");
          pst.setInt(1,codenv);
          pst.setInt(2,cod);
      

       
         int r = pst.executeUpdate();
       if (r != 0) {
          res = true;
       }

    
   

        return res;
    }





       public List<Pedido> listarCod(int cod) throws SQLException {

              Connection conex=model.getCon();
        if (!model.isConnect())
		throw new SQLException("no hay conexcion");

        PreparedStatement ps = null;
        ResultSet rs = null;
        Pedido c= null;
        List lista = new LinkedList();
        try {
            ps =(PreparedStatement)  conex.prepareStatement("select * from pedidos where codigo_pedidos like '%"+cod+"%'");
            rs = ps.executeQuery();

            while (rs.next()) {
                c=(Pedido) load(rs);
                lista.add(c);
            }

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }




       public List<Pedido> listarNom(int cod) throws SQLException {



        Connection conex=model.getCon();
        if (!model.isConnect())
		throw new SQLException("no hay conexcion");

        PreparedStatement ps = null;
        ResultSet rs = null;
        Pedido c= null;
        List lista = new LinkedList();
        try {
            ps =(PreparedStatement)  conex.prepareStatement("select * from pedidos where codClie like '%"+cod+"%'");
            rs = ps.executeQuery();

            while (rs.next()) {
                c=(Pedido) load(rs);
                lista.add(c);
            }

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }





     public  int NumRegistros()throws SQLException{
        int numfil=0;
            if (model.getCon() == null ) {
          throw new SQLException(" no hay conexion ");
         }
         ResultSet rs = null;
          PreparedStatement pst = null;
           try {
            pst = model.getCon().prepareStatement("select count(*) AS  numfil from pedidos");
            rs = pst.executeQuery();

             while(rs.next()) {
             numfil = rs.getInt("numfil");
         }
            }finally {
        if (pst != null) {
             pst.close();
        }

   }
         return numfil;
    }

       public  int IncrementarCod()throws SQLException{
        int cod=0;

            if (model.getCon() == null ) {
          throw new SQLException(" no hay conexion ");
         }
         ResultSet rs = null;
          PreparedStatement pst = null;
           try {
            pst = model.getCon().prepareStatement("select max(codigo_pedidos)+1 AS codInt from pedidos");
            rs = pst.executeQuery();

             while(rs.next()) {
             cod= rs.getInt("codInt");
         }
            }finally {
        if (pst != null) {
             pst.close();
        }

   }
         return cod;
    }

        public List<Pedido> getAll() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }




    public Pedido find(Integer id) throws SQLException {
                  Connection conex=(Connection) model.getCon();

                if (!model.isConnect())
			throw new SQLException("no hay conexcion");
                Pedido pe= new Pedido();
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=(PreparedStatement) conex.prepareStatement("SELECT * FROM clientes WHERE Identificacion=?");
			rs=pstmt.executeQuery();
			if(rs.next()){


                        }else{

                        }
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return pe;
    }



      public Pedido load(ResultSet rs) throws SQLException {

          ClienteDAO cdao= new ClienteDAO();

        Pedido c = new Pedido();
           c.setCodigo(rs.getInt(1));
        Integer idc= new Integer(rs.getInt(2));
         c.setCliente(cdao.find(idc));


        c.setFechadelpedido(rs.getDate(3));
        c.setFechaentrga(rs.getDate(4));
        c.setSubtotal(rs.getDouble(5));

        
          c.setIva(rs.getDouble(6));
          c.setTotal(rs.getDouble(7));
          
         c.setEstado(rs.getString(8));

         



        return c;
    }

      public List<Pedido> listar() throws SQLException {
              Connection conex=model.getCon();
        if (!model.isConnect())
		throw new SQLException("no hay conexcion");

        PreparedStatement ps = null;
        ResultSet rs = null;
        Pedido c= null;
        List lista = new LinkedList();
        try {

            ps =(PreparedStatement)  conex.prepareStatement("select * from pedidos ");

            rs = ps.executeQuery();

            while (rs.next()) {
                c=(Pedido) load(rs);
                lista.add(c);
            }

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }


    public void delete(Pedido t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void update(Pedido t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
